home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / VFSCANF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  1.5 KB  |  45 lines

  1. /* vfscanf.c --- p 476 */
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. void getfromfile(FILE *, char *,...);
  5. main()
  6. {
  7.     char filename[80], dbname[80], pname[80],
  8.         mname[80], partno[40];
  9.     FILE *datafile;
  10.     double uprice;
  11.                 /* Get file name and open it for reading */
  12.     printf("Enter name of data file:");
  13.     gets(filename);
  14.     if((datafile = fopen(filename, "r")) == NULL)
  15.     {
  16.        printf("Error opening file: %s\n", filename);
  17.        exit(1);
  18.     }
  19.         /* Call the input routine to read values of variables.
  20.          * First just a single number. Then more than one value. */
  21.     getfromfile(datafile, "DATABASE: %[^\n] ", &dbname);
  22.     getfromfile(datafile, "Product Name: %[^\n] Manufacturer: %[^\n] "
  23.             "Part #: %s Unit Price: $%lf",
  24.                       &pname, &mname, &partno, &uprice);
  25.     printf("The first record in %s contain:\n Product Name: %s\n"
  26.           "Manufacturer: %s\n"
  27.           "Part #:       %s\n"
  28.           "Unit Price:   $%.2lf\n", dbname, pname,
  29.           mname, partno, uprice);
  30. }
  31.                         /*-------------------------*/
  32.             /*  getfromfile: accepts variable number of arguments
  33.             *  and reads formatted values from a file */
  34. void getfromfilef(FILE *stream, char *my_format,...)
  35. {
  36.     va_list arg_pointer;
  37.             /* Use va_start macro to get to the start of the
  38.              * variable number of arguments. This will alter the
  39.              * pointer arg_pointer to point to the list of
  40.              * variables to be read. */
  41.     va_startf(arg_pointer, my_format);
  42.     vfscanf(stream, my_format, arg_pointer);
  43.             /* Use the va_end macro to reset the arg_pointer */
  44.     va_end(arg_pointer);
  45. }